#! /bin/sh

#---------------------------------------------------------------------
#                                init                                 
# 
# This is called by the template init method, which is itself called
# by fw-init when a project is being initialized.  It is intended
# for creating files in the project that will remain in the project
# and are owned by the project.  Creation of (symlinks to) files
# owned by the build method should be done at bootstrap time.
#---------------------------------------------------------------------

#---------------------------------------------------------------------
#                           chicken and egg                           
#---------------------------------------------------------------------

which perl >/dev/null 2>/dev/null || {
  echo "javascript/init: fatal: can't locate perl" 1>&2
  exit 1
}

if test -z "${FW_ROOT}"
  then
    FW_ROOT=`echo "$0" | perl -pe 's%fw/template/javascript/init$%%;'`
    export FW_ROOT
  fi

test -f "${FW_ROOT}/share/fw/sh/fw-find.sh" || {
  echo "javascript/init: fatal: can't determine FW_ROOT" 1>&2
  echo "javascript/init: please set the environment variable" 1>&2
  exit 1
}

. "${FW_ROOT}/share/fw/sh/fw-find.sh"

#---------------------------------------------------------------------
#                              routines                               
#---------------------------------------------------------------------

fw_source "javascript/init" "sh/fw-exec.sh"
fw_source "javascript/init" "sh/parse-args.sh"
fw_source "javascript/init" "sh/validate-args.sh"

#---------------------------------------------------------------------
#                                main                                 
#---------------------------------------------------------------------

parse_args "javascript/init" "$@"

validate_args "javascript/init" "name" ""

for dir in src tests
  do
    mkdir "$dir" || fatal "javascript/init"
    fw_exec "revision/$revision/add-dir" "$dir" || fatal "javascript/init"

    for x in Makefile.am.local
      do
        fw_find "template/javascript/$dir/$x" file
        test -z "$file" && fatal "javascript/init"
        cp -f "$file" "$dir" || fatal "javascript/init"
        chmod +w "$dir/$x" || fatal "javascript/init"
      done

    for x in qunit-testmylib.js testmylib.jstestdriver.js testmylib.js mylib.js 
      do
        fw_find "template/javascript/$dir/$x" file
        test -z "$file" || {
          cp -f "$file" "$dir" || fatal "javascript/init"
          chmod +w "$dir/$x" || fatal "javascript/init"
        }
      done

    (
      cd "$dir" &&                                              \
      fw_exec "revision/$revision/add-files"                    \
        Makefile.am.local                                       \
        qunit-testmylib.js                                      \
        testmylib.js                                            \
        testmylib.jstestdriver.js                               \
        mylib.js &&                                             \
      fw_exec "revision/$revision/ignore-files"                 \
        Makefile                                                \
        '*.in'                                                  \
        '.*.js_ok'
    ) || fatal "javascript/init"
  done

(cd src &&                                                      \
 fw_exec "revision/$revision/ignore-files"                      \
   "${name}.js"                                                 \
   "${name}-uncompressed.js"
) || fatal "javascript/init"

(cd tests &&                                                    \
 fw_exec "revision/$revision/ignore-files"                      \
   '*.test.out'						        \
   'jscoverage*'                                                \
   'jquery-1.3.2.js'                                            \
   'js-test-wrapper.sh'                                         \
   'jstestdriver-*'                                             \
   'testrunner-2009-09-13.js'					\
) || fatal "javascript/init"

(
  cd fw-pkgin &&                                                \
  perl -i -pe 's/FW_PACKAGE_ARCHITECTURE_DEPENDENT="1"/FW_PACKAGE_ARCHITECTURE_DEPENDENT="0"/' config
  perl -i -pe 's/FW_PACKAGE_BUILD_DEPENDS=""/FW_PACKAGE_BUILD_DEPENDS="firefox [!darwin-i386], xvfb [!darwin-i386] | xorg-x11-server-Xvfb [!darwin-i386]"/' config
) || fatal "javascript/init"

(
  cd fw-pkgin &&                                                \
  cat <<'EOD' >> config

# whether js-test-wrapper.sh should use Xvfb to provide X-windows to 
# browsers invoked during testing.  valid choices are yes, no, or auto.
# auto will start Xvfb if the DISPLAY environment variable is not set.
FW_TEMPLATE_JAVASCRIPT_USE_XVFB="auto"

# if Xvfb is invoked, what display number should be used
FW_TEMPLATE_JAVASCRIPT_XVFB_DISPLAY=":1"

# if jstestdriver or netcat is invoked, what port number should be used
# should either be an integer, or auto.  auto attempts to guess an
# unused port.
FW_TEMPLATE_JAVASCRIPT_PORT="auto"

# if jstestdriver or qunit is invoked, what browsers should be used.  
# should be a list of comma seperated list of browser executable names.
FW_TEMPLATE_JAVASCRIPT_BROWSER="firefox"

# extra per-browser arguments.  for example, uncomment to instruct firefox 
# to use the profile called testing, which you should have previously
# set up with the profile manager
#FW_TEMPLATE_JAVASCRIPT_BROWSER_ARGS_firefox="-P testing"

# if jstestdriver is invoked, a jsTestDriver.conf is created by
# js-test-wrapper.sh which loads src/@FW_PACKAGE_NAME@-uncompressed.js 
# and tests/*.jstestdriver.js .  you can load additional files by indicating 
# a comma-prefixed, comma-seperated list of JSON strings 
# (so you should single quote the entire variable and then 
# double-quote the individual paths)
FW_TEMPLATE_JAVASCRIPT_CONF_EXTRA=''

EOD
) || fatal "javascript/init"

fw_exec "build/automake/init" "$@"
